package com.controllers;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.service.Commonmethod;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

//https://segmentfault.com/a/1190000011428318  子树查询
//https://www.cnblogs.com/azhqiang/p/4169534.html
@RequestMapping("/goods")
public class Goods extends JbootController {


    private com.model.Goods goodsobj = new com.model.Goods();
    private com.model.GoodsCollect goodscollect = new com.model.GoodsCollect();

    public void save() {


        com.model.Goods  goods = goodsobj.dao.findById(23);

        System.out.println(goods);
        String ss = getPara();

        System.out.println(ss);
        JSONObject obj = new JSONObject();
        obj.put("name", "foo");
        obj.put("num", new Integer(100));
        obj.put("balance", new Double(1000.21));
        obj.put("is_vip", new Boolean(true));
        renderJson(obj);

    }


    //关注藏品
    public void likeandcollect() {
        Integer goods_id = getParaToInt("goods_id");
        com.model.Goods  goods = goodsobj.dao.findById(goods_id);
        System.out.println(goods);
        JSONObject obj = new JSONObject();
       Integer rand_user_id=  Commonmethod.getuserid();
        System.out.println(rand_user_id);
        if(rand_user_id>0){
            // 获取user的name属性
            Integer user_id = goods.getInt("user_id");
            Integer end_time = goods.getInt("endTime");
            long add_time  =System.currentTimeMillis()/1000;

            if(end_time<add_time){
                obj.put("code", "200");
                obj.put("meggage", "good is exprite");
                renderJson(obj);
                return;
            }
            Map map = new HashMap();
            map.put("user_gooods_id", user_id);
            map.put("user_id", rand_user_id);
            map.put("goods_id", goods_id);
            map.put("add_time", add_time);
            System.out.println(goodscollect.use("a1").put(map).save());
         //   System.out.println(goodscollect.us.put(map).save());
            Random rand = new Random();
            int randNumber =rand.nextInt(10) + 1;
            System.out.println(Db.update(" UPDATE tp_goods SET click_count=click_count+"+randNumber+" WHERE goods_id="+goods_id));
            obj.put("name", "foo");
            obj.put("num", new Integer(100));
            obj.put("balance", new Double(1000.21));
            obj.put("is_vip", new Boolean(true));
            renderJson(obj);
        }else{
            obj.put("code", "200");
            obj.put("meggage", "goods");
            renderJson(obj);
        }

    }


}
